<?php
// 保存系统设置到settings.json文件

header('Content-Type: application/json');

// 只处理POST请求
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['status' => 'error', 'message' => '只允许POST请求']);
    exit;
}

// 获取POST数据
$input = file_get_contents('php://input');
$settings = json_decode($input, true);

// 检查数据格式
if (!$settings) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => '无效的数据格式']);
    exit;
}

try {
    // 将设置保存到settings.json文件
    $jsonContent = json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    $result = file_put_contents('settings.json', $jsonContent);
    
    if ($result !== false) {
        echo json_encode(['status' => 'success', 'message' => '设置保存成功']);
    } else {
        throw new Exception('无法写入文件');
    }
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => '保存设置失败: ' . $e->getMessage()]);
}
?>